﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;
using BusinessLogicLayer;
using DataAccessLayer;
using System.Drawing;
using System.Globalization;

namespace ChamCong.LanhDao
{
    public partial class Chart : BasePage
    {
        public static int temp;
        public static string str;
        CongBLL c = new CongBLL();
        NhanVienBLL nv = new NhanVienBLL();
        int _MaNV;
        List<int> lstRole;
        QuanLyPhanQuyenBLL pqBLL = new QuanLyPhanQuyenBLL();
        PhongBanBLL pbBLL = new PhongBanBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            _MaNV = Convert.ToInt32(Session["MaNV"]);
            lstRole = pqBLL.LayMaRoleTuNV(_MaNV);
            if (!Page.IsPostBack)
            {
                bindDdlYear();
                binDdlDept();
                bindCblEmp(int.Parse(ddlDept.SelectedValue));
            }
            
        }
        
        public void bindChartByMultiId()
        {
            int _Thang = int.Parse(ddlMonth.SelectedValue);
            int _Nam = int.Parse(ddlYear.SelectedValue);
            
            System.Web.UI.DataVisualization.Charting.Chart chartTest = new System.Web.UI.DataVisualization.Charting.Chart();

            chartTest.Width = 1150;
            chartTest.Height = 350;
            chartTest.RenderType = RenderType.ImageTag;
            chartTest.ImageLocation = "..\\..\\TempImages\\ChartPic_#SEQ(200,30)";

            chartTest.Palette = ChartColorPalette.BrightPastel;
            Title t = new Title("Biểu đồ Thống kê Giờ vào làm của nhân viên\n Tháng " + _Thang + "/" + _Nam, Docking.Top, new Font("Segoe UI", 14, FontStyle.Bold),Color.FromArgb(26, 59, 105));
            chartTest.Titles.Add(t);

            chartTest.ChartAreas.Add("ChartArea1");
            chartTest.ChartAreas["ChartArea1"].AxisY.LabelStyle.Format = "HH:mm";
            chartTest.ChartAreas["ChartArea1"].AxisY.IntervalType = DateTimeIntervalType.Seconds;

            chartTest.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "dd/MM";
            chartTest.ChartAreas["ChartArea1"].AxisX.Interval = 1;
            chartTest.ChartAreas["ChartArea1"].AxisX.IntervalType = DateTimeIntervalType.Days;

            chartTest.ChartAreas["ChartArea1"].AxisX.MajorGrid.Enabled = false;

            //chartTest.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
            //chartTest.ChartAreas["ChartArea1"].AxisY.Maximum = 43200;
            chartTest.ChartAreas["ChartArea1"].AxisY.MajorGrid.Enabled = false;
            //chartTest.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineWidth = 1;
            //chartTest.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineColor = Color.Red;//FromArgb(240, 240, 240);
            //chartTest.ChartAreas["ChartArea1"].AxisY.MajorGrid.Interval = 3000;
            //chartTest.ChartAreas["ChartArea1"].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dot;
            //chartTest.ChartAreas["ChartArea1"].AxisX.IsMarginVisible = false;
            

            for (int i = 0; i < cblDSNV.Items.Count; i++)
            {
                if (cblDSNV.Items[i].Selected == true)
                {

                    List<CONG> lst = c.LayCongTheoThang(_Thang, _Nam, int.Parse(cblDSNV.Items[i].Value));
                    string seriesName = nv.LayNhanVien(int.Parse(cblDSNV.Items[i].Value)).HoTen + " (" + nv.LayNhanVien(int.Parse(cblDSNV.Items[i].Value)).MaNV + ")";
                    chartTest.Series.Add(seriesName);
                    for (int j = 0; j < lst.Count; j++)
                    {
                        DateTime dtm = DateTime.Parse(lst[j].GioDen.ToString());
                        DateTime d = DateTime.Parse(lst[j].Ngay.ToString());
                        chartTest.Series[seriesName].Points.AddXY(d, dtm.ToOADate() - d.ToOADate());                       
                        chartTest.Series[seriesName].IsValueShownAsLabel = false;
                        chartTest.Series[seriesName].ToolTip = "Nhân viên: " + seriesName + "\nNgày: #VALX{dd/MM/yyyy}\nGiờ vào: #VALY";
                        //chartTest.Series[seriesName].MapAreaAttributes = "onmouseover=" + "showTooltip('#AVG');";
                    }
                    chartTest.Series[seriesName].BorderWidth = 1;
                    chartTest.Series[seriesName].ChartType = SeriesChartType.Spline;
                    chartTest.Series[seriesName].YValueType = ChartValueType.Time;
                    chartTest.Series[seriesName].XValueType = ChartValueType.Date;
                    chartTest.Series[seriesName].MarkerStyle = MarkerStyle.Circle;
                    chartTest.Series[seriesName].MarkerSize = 3;
                    chartTest.Series[seriesName].MarkerColor = Color.Black;

                    
                }
            }
            foreach (Series series in chartTest.Series)
            {
                series.EmptyPointStyle.Color = Color.Gray;
                series.EmptyPointStyle.BorderWidth = 2;
                series.EmptyPointStyle.BorderDashStyle = ChartDashStyle.Dash;
                series.EmptyPointStyle.MarkerSize = 7;
                series.EmptyPointStyle.MarkerStyle = MarkerStyle.Cross;
                series.EmptyPointStyle.MarkerBorderColor = Color.Black;
                series.EmptyPointStyle.MarkerColor = Color.LightGray;

            }
            //chartTest.Series["Series 1"].IsValueShownAsLabel = true;
            chartTest.BorderSkin.SkinStyle = BorderSkinStyle.Emboss;
            chartTest.BorderColor = System.Drawing.Color.FromArgb(26, 59, 105);
            chartTest.BorderlineDashStyle = ChartDashStyle.Solid;
            chartTest.BorderWidth = 1;

            chartTest.Legends.Add("Legend1");
            chartTest.Legends["Legend1"].Enabled = true;
            chartTest.Legends["Legend1"].Docking = Docking.Bottom;
            //str = "start";

            Panel1.Controls.Add(chartTest);
            Response.Write(str);
        }
        public void bindCblEmp(int _MaPB)
        {
            if (lstRole.Contains(1) || lstRole.Contains(2))
            {
                cblDSNV.DataSource = nv.LayTatCa().Where(p => p.MaPB == _MaPB).ToList();
            }
            else
            {
                cblDSNV.DataSource = nv.LayTatCa().Where(p => p.MaPB == _MaPB && p.MaNV == _MaNV).ToList();
            }                   
            
            cblDSNV.DataTextField = "HoTen";
            cblDSNV.DataValueField = "MaNV";
            cblDSNV.DataBind();
        }
        
        public void bindDdlYear()
        {
            for (int i = DateTime.Now.Year; i >= 2010; i--)
            {
                ListItem obj = new ListItem();
                obj.Text = i.ToString();
                obj.Value = i.ToString();
                ddlYear.Items.Add(obj);
            }
            for (int i = 1; i <= 12; i++)
            {
                ListItem item = new ListItem();
                item.Text = "Tháng " + i;
                item.Value = i.ToString();
                ddlMonth.Items.Add(item);
            }
        }
        public void binDdlDept() 
        {

            if (lstRole.Contains(1))
            {
                ddlDept.DataSource = pbBLL.LayTatCa().ToList();
            }
            else
            {
                ddlDept.DataSource = pbBLL.LayTatCa().Where(p => p.MaPB == nv.LayMaPBTuNV(_MaNV)).ToList();
            }           
            
            ddlDept.DataTextField = "TenPB";
            ddlDept.DataValueField = "MaPB";
            ddlDept.DataBind();
        }
        public int countCheckedItems()
        {
            int count = 0;
            for (int i = 0; i < cblDSNV.Items.Count; i++)
            {
                if (cblDSNV.Items[i].Selected == true)
                {
                    count++;
                }
            }
            return count;
        }
        protected void cblDSNV_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (countCheckedItems() > 0)
            {
                this.bindChartByMultiId();
                lblNotice.Text = "";
                imgPreloaderLine.Visible = false;
            }
            else
            {
                lblNotice.Text = "Chọn nhân viên";
                imgPreloaderLine.Visible = true;
            }

        }

        protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (countCheckedItems() > 0)
            {
                this.bindChartByMultiId();
            }
            else
            {
                lblNotice.Text = "Chọn nhân viên";
                imgPreloaderLine.Visible = true;
            }
            
        }

        protected void ddlYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (countCheckedItems() > 0)
            {
                this.bindChartByMultiId();
            }
            else
            {
                lblNotice.Text = "Chọn nhân viên";
                imgPreloaderLine.Visible = true;
            }
        }

        protected void ddlDept_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindCblEmp(int.Parse(ddlDept.SelectedValue));
        }

    }
}